home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / mint104s.zoo / mint.src / dos.c < prev    next >
C/C++ Source or Header  |  1993-03-08  |  13KB  |  583 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* miscellaneous DOS functions, and the DOS initialization function */
  8.  
  9. #include "mint.h"
  10.  
  11. #define DOS_MAX 0x140
  12.  
  13. Func dos_tab[DOS_MAX];
  14. short dos_max = DOS_MAX;
  15.  
  16. static void alarmme P_((PROC *));
  17.  
  18. long ARGS_ON_STACK 
  19. s_version()
  20. {
  21.     return Sversion();
  22. }
  23.  
  24. /*
  25.  * Super(new_ssp): change to supervisor mode.
  26.  */
  27.  
  28. long ARGS_ON_STACK
  29. s_uper(new_ssp)
  30.     long new_ssp;
  31. {
  32.     int in_super;
  33.     long r;
  34.  
  35.     TRACE(("Super"));
  36.     in_super = curproc->ctxt[SYSCALL].sr & 0x2000;
  37.  
  38.     if (new_ssp == 1) {
  39.         r = in_super ? -1L : 0;
  40.     }
  41.     else {
  42.         curproc->ctxt[SYSCALL].sr ^= 0x2000;
  43.         r = curproc->ctxt[SYSCALL].ssp;
  44.         if (in_super) {
  45.             if (new_ssp == 0) {
  46.                 DEBUG(("bad Super call"));
  47.                 raise(SIGSYS);
  48.             }
  49.             else {
  50.                 curproc->ctxt[SYSCALL].usp = 
  51.                     curproc->ctxt[SYSCALL].ssp;
  52.                 curproc->ctxt[SYSCALL].ssp = new_ssp;
  53.             }
  54.         }
  55.         else {
  56.             curproc->ctxt[SYSCALL].ssp = 
  57.                 new_ssp ? new_ssp : curproc->ctxt[SYSCALL].usp;
  58.         }
  59.     }
  60.     return r;
  61. }
  62.  
  63. /*
  64.  * get/set time and date functions
  65.  */
  66. long ARGS_ON_STACK t_getdate() { return datestamp; }
  67. long ARGS_ON_STACK t_gettime() { return timestamp; }
  68.  
  69. long ARGS_ON_STACK t_setdate(date)
  70.     int date;
  71. {
  72.     long r = Tsetdate(date);
  73.     datestamp = Tgetdate();
  74.     return r;
  75. }
  76.  
  77. long ARGS_ON_STACK t_settime(time)
  78.     int time;
  79. {
  80.     long r = Tsettime(time);
  81.     timestamp = Tgettime();
  82.     return r;
  83. }
  84.  
  85. /*
  86.  * GEMDOS extension: Syield(): give up the processor if any other
  87.  * processes are waiting. Always returns 0.
  88.  */
  89.  
  90. long ARGS_ON_STACK
  91. s_yield()
  92. {
  93. /* reward the nice process */
  94.     curproc->curpri = curproc->pri;
  95.     sleep(READY_Q, curproc->wait_cond);
  96.     return 0;
  97. }
  98.  
  99. /*
  100.  * GEMDOS extension:
  101.  * Prenice(pid, delta) sets the process priority level for process pid.
  102.  * A "nice" value < 0 increases priority, one > 0 decreases it.
  103.  * Always returns the new priority (so Prenice(pid, 0) queries the current
  104.  * priority).
  105.  *
  106.  * NOTE: for backward compatibility, Pnice(delta) is provided and is equivalent
  107.  * to Prenice(Pgetpid(), delta)
  108.  */
  109.  
  110. long ARGS_ON_STACK
  111. p_renice(pid, delta)
  112.     int pid, delta;
  113. {
  114.     PROC *p;
  115.  
  116.     if (pid <= 0 || 0 == (p = pid2proc(pid))) {
  117.         return EFILNF;
  118.     }
  119.  
  120.     if (curproc->euid && curproc->euid != p->ruid
  121.         && curproc->ruid != p->ruid) {
  122.         DEBUG(("Prenice: process ownership error"));
  123.         return EACCDN;
  124.     }
  125.     p->pri -= delta;
  126.     if (p->pri < MIN_NICE) p->pri = MIN_NICE;
  127.     if (p->pri > MAX_NICE) p->pri = MAX_NICE;
  128.     p->curpri = p->pri;
  129.     return ((long)p->pri) & 0x0ffff;
  130. }
  131.  
  132. long ARGS_ON_STACK
  133. p_nice(delta)
  134.     int delta;
  135. {
  136.     return p_renice(curproc->pid,delta);
  137. }
  138.  
  139. /*
  140.  * GEMDOS extensions: routines for getting/setting process i.d.'s and
  141.  * user i.d.'s
  142.  */
  143.  
  144. long ARGS_ON_STACK p_getpid() { return curproc->pid; }
  145.  
  146. long ARGS_ON_STACK p_getppid() { return curproc->ppid; }
  147.  
  148. long ARGS_ON_STACK p_getpgrp() { return curproc->pgrp; }
  149.  
  150. /* note: Psetpgrp(0, ...) is equivalent to Psetpgrp(Pgetpid(), ...) */
  151. /* also note: Psetpgrp(x, 0) is equivalent to Psetpgrp(x, x) */
  152.  
  153. long ARGS_ON_STACK p_setpgrp(pid, newgrp)
  154.     int pid, newgrp;
  155. {
  156.     PROC *p;
  157.  
  158.     if (pid == 0)
  159.         p = curproc;
  160.     else if (0 == (p = pid2proc(pid)))
  161.         return EFILNF;
  162.     if ( (curproc->euid) && (p->ruid != curproc->ruid)
  163.           && (p->ppid != curproc->pid) )
  164.         return EACCDN;
  165.  
  166.     if (newgrp < 0)
  167.         return p->pgrp;
  168.  
  169.     if (newgrp == 0)
  170.         newgrp = p->pid;
  171.  
  172.     return (p->pgrp = newgrp);
  173. }
  174.  
  175. long ARGS_ON_STACK p_getuid() { return curproc->ruid; }
  176. long ARGS_ON_STACK p_getgid() { return curproc->rgid; }
  177. long ARGS_ON_STACK p_geteuid() { return curproc->euid; }
  178. long ARGS_ON_STACK p_getegid() { return curproc->egid; }
  179.  
  180. long ARGS_ON_STACK
  181. p_setuid(id)
  182.     int id;
  183. {
  184.     if (curproc->euid == 0 || curproc->ruid == id) {
  185.         curproc->ruid = curproc->euid = id;
  186.         return id;
  187.     }
  188.     return EACCDN;
  189. }
  190.  
  191. long ARGS_ON_STACK
  192. p_setgid(id)
  193.     int id;
  194. {
  195.     if (curproc->euid == 0 || curproc->egid == 0 || curproc->rgid == id) {
  196.         curproc->egid = curproc->rgid = id;
  197.         return id;
  198.     }
  199.     return EACCDN;
  200. }
  201.  
  202. /*
  203.  * a way to get/set process-specific user information. the user information
  204.  * longword is set to "arg", unless arg is -1. In any case, the old
  205.  * value of the longword is returned.
  206.  */
  207.  
  208. long ARGS_ON_STACK
  209. p_usrval(arg)
  210.     long arg;
  211. {
  212.     long r;
  213.  
  214.     TRACE(("Pusrval"));
  215.     r = curproc->usrdata;
  216.     if (arg != -1L)
  217.         curproc->usrdata = arg;
  218.     return r;
  219. }
  220.  
  221. /*
  222.  * set the file creation mask to "mode". Returns the old value of the
  223.  * mask.
  224.  */
  225. long ARGS_ON_STACK p_umask(mode)
  226.     unsigned mode;
  227. {
  228.     long oldmask = curproc->umask;
  229.  
  230.     curproc->umask = mode & (~S_IFMT);
  231.     return oldmask;
  232. }
  233.  
  234. /*
  235.  * get/set the domain of a process. domain 0 is the default (TOS) domain.
  236.  * domain 1 is the MiNT domain. for now, domain affects read/write system
  237.  * calls and filename translation.
  238.  */
  239.  
  240. long ARGS_ON_STACK
  241. p_domain(arg)
  242.     int arg;
  243. {
  244.     long r;
  245.     TRACE(("Pdomain(%d)", arg));
  246.  
  247.     r = curproc->domain;
  248.     if (arg >= 0)
  249.         curproc->domain = arg;
  250.     return r;
  251. }
  252.  
  253. /*
  254.  * get process resource usage. 8 longwords are returned, as follows:
  255.  *     r[0] == system time used by process
  256.  *     r[1] == user time used by process
  257.  *     r[2] == system time used by process' children
  258.  *     r[3] == user time used by process' children
  259.  *     r[4] == memory used by process
  260.  *     r[5] - r[7]: reserved for future use
  261.  */
  262.  
  263. long ARGS_ON_STACK
  264. p_rusage(r)
  265.     long *r;
  266. {
  267.     r[0] = curproc->systime;
  268.     r[1] = curproc->usrtime;
  269.     r[2] = curproc->chldstime;
  270.     r[3] = curproc->chldutime;
  271.     r[4] = memused(curproc);
  272.     return 0;
  273. }
  274.  
  275. /*
  276.  * get/set resource limits i to value v. The old limit is always returned;
  277.  * if v == -1, the limit is unchanged, otherwise it is set to v. Possible
  278.  * values for i are:
  279.  *    1:  max. cpu time    (milliseconds)
  280.  *    2:  max. core memory allowed
  281.  *    3:  max. amount of malloc'd memory allowed
  282.  */
  283. long ARGS_ON_STACK
  284. p_setlimit(i, v)
  285.     int i;
  286.     long v;
  287. {
  288.     long oldlimit;
  289.  
  290.     switch(i) {
  291.     case 1:
  292.         oldlimit = curproc->maxcpu;
  293.         if (v >= 0) curproc->maxcpu = v;
  294.         break;
  295.     case 2:
  296.         oldlimit = curproc->maxcore;
  297.         if (v >= 0) {
  298.             curproc->maxcore = v;
  299.             recalc_maxmem(curproc);
  300.         }
  301.         break;
  302.     case 3:
  303.         oldlimit = curproc->maxdata;
  304.         if (v >= 0) {
  305.             curproc->maxdata = v;
  306.             recalc_maxmem(curproc);
  307.         }
  308.         break;
  309.     default:
  310.         DEBUG(("Psetlimit: invalid mode %d", i));
  311.         return EINVFN;
  312.     }
  313.     TRACE(("p_setlimit(%d, %ld): oldlimit = %ld", i, v, oldlimit));
  314.     return oldlimit;
  315. }
  316.  
  317. /*
  318.  * pause: just sleeps on IO_Q, with wait_cond == -1. only a signal will
  319.  * wake us up
  320.  */
  321.  
  322. long ARGS_ON_STACK
  323. p_pause()
  324. {
  325.     TRACE(("Pause"));
  326.     sleep(IO_Q, -1L);
  327.     return 0;
  328. }
  329.  
  330. /*
  331.  * helper function for t_alarm: this will be called when the timer goes
  332.  * off, and raises SIGALRM
  333.  */
  334.  
  335. static void
  336. alarmme(p)
  337.     PROC *p;
  338. {
  339.     p->alarmtim = 0;
  340.     post_sig(p, SIGALRM);
  341. }
  342.  
  343. /*
  344.  * t_alarm(x): set the alarm clock to go off in "x" seconds. returns the
  345.  * old value of the alarm clock
  346.  */
  347.  
  348. long ARGS_ON_STACK
  349. t_alarm(x)
  350.     long x;
  351. {
  352.     long oldalarm;
  353.     TIMEOUT *t;
  354.  
  355. /* see how many milliseconds there were to the alarm timeout */
  356.     oldalarm = 0;
  357.  
  358.     if (curproc->alarmtim) {
  359.         for (t = tlist; t; t = t->next) {
  360.             oldalarm += t->when;
  361.             if (t == curproc->alarmtim)
  362.                 goto foundalarm;
  363.         }
  364.         DEBUG(("Talarm: old alarm not found!"));
  365.         oldalarm = 0;
  366.         curproc->alarmtim = 0;
  367. foundalarm:
  368.         ;
  369.     }
  370.  
  371.     oldalarm = (oldalarm+999) / 1000;    /* convert to seconds */
  372.  
  373. /* we were just querying the alarm */
  374.     if (x < 0)
  375.         return oldalarm;
  376.  
  377. /* cancel old alarm */
  378.     if (curproc->alarmtim)
  379.         canceltimeout(curproc->alarmtim);
  380.  
  381. /* add a new alarm, to occur in 1000*x milliseconds */
  382.     if (x)
  383.         curproc->alarmtim = addtimeout(1000*x, alarmme);
  384.     else
  385.         curproc->alarmtim = 0;
  386.  
  387.     return oldalarm;
  388. }
  389.  
  390. /*
  391.  * sysconf(which): returns information about system configuration.
  392.  * "which" specifies which aspect of the system configuration is to
  393.  * be returned:
  394.  *    -1    max. value of "which" allowed
  395.  *    0    max. number of memory regions per proc
  396.  *    1    max. length of Pexec() execution string {ARG_MA